const app = getApp();
export const AGREE = 'agree'
export const USER_TOKEN_KEY = 'userToken'
export const USERLOCATION = 'userLocation'
export const USER_ID = 'userId'
export const USER_ROLES = 'userRoles'
export const USER_INFO = 'userInfo'
export const HAS_USER_INFO = 'hasUserInfo' //缓存登录状态
export const NETWORKINFO = 'networkInfo' //缓存网点信息
export const SHOPPINSELECT = 'shoppinSelect' //缓存网点信息
export const SHOPPINGCART = 'shoppingCart'
import * as api from '@/common/api'
import store from '@/store'
import * as ResponseStatus from '@/common/response-status.js'
//价格处理
export const priceFilter =(sellPrice, index) =>{
	if (sellPrice > 0) {
	  sellPrice += "";
	  let price = sellPrice.split(".");
	  if (price.length < 2) {
		price.push("0");
	  }
	  return price[index];
	} else {
	  return 0;
	}
}

export const refreshToken =() =>{
	let token =""
	uni.showLoading({
	  title: '重新登录中',
	  mask: true
	})
	uni.login({
	  success: (res)=> {
	    /**获取unionid**/
		  console.log("登录", res.code)
		  api.login({code:res.code}).then((res) => {
			  uni.hideLoading();
			  console.log("登录", res.data.data)
			  if(res.data.code == ResponseStatus.OK){
				  store.commit('setToken', res.data.data)
				  uni.setStorageSync(USER_TOKEN_KEY,res.data.data)
				  const pages = getCurrentPages()
				  // 声明一个pages使用getCurrentPages方法
				  const curPage = pages[pages.length - 1]
				  // 声明一个当前页面
				  curPage.onLoad(curPage.options) // 传入参数
				  curPage.onShow()
				  curPage.onReady()
			  }
		  });
	  }
	})
}

/** 验证手机号格式
 * @param {string} str
 * @returns {Boolean} 
 */
export function validPhone(str) {
  let reg = /^1[3456789]\d{9}$/
  return reg.test(str)
}

/**
 * token失效
 */
export function invalidToken(){
	uni.removeStorageSync(USER_TOKEN_KEY)
	uni.removeStorageSync(HAS_USER_INFO)
}
/**
 * 显示提示信息
 */
export function showInfoToast(title){
	uni.showToast({
		title: title,
		icon: 'none',
		duration: 2000
	})
}

//获取当前年月日时分秒
export const getTimes = (time) => {
	let date = new Date();
	if (time) {
		date = new Date(time);
	}
	var year = date.getFullYear(),
		month = date.getMonth() + 1,
		day = date.getDate(),
		hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(),
		minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(),
		second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
	month >= 1 && month <= 9 ? (month = "0" + month) : "";
	day >= 0 && day <= 9 ? (day = "0" + day) : "";
	// var timer = year + '-' + month + '-' + day;
	var timer = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
	return timer;
}